package cn.wolfcode.shiro;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sound.sampled.Line;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class CrmRealm extends AuthorizingRealm {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    public void setCredentialsMatcher (CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

    protected AuthenticationInfo doGetAuthenticationInfo (AuthenticationToken token) throws AuthenticationException {
        //通过token获取用户名
        Object username = token.getPrincipal();
        //判断是否存在数据库
        Employee employee = employeeMapper.selectByName((String) username);


        if (employee != null) {
            //身份信息,凭证信息,盐三个参数
            return new SimpleAuthenticationInfo(employee, employee.getPassword(),
                    ByteSource.Util.bytes(username), "CrmRealm");
        }
        return null;
    }

    protected AuthorizationInfo doGetAuthorizationInfo (PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        /*info.addRole("HR_MGR");
        info.addStringPermissions(Arrays.asList("employee:list","role:list"));*/

        //获取登录用户(主体)
        Subject subject = SecurityUtils.getSubject();
        //获取登录用户身份信息(对象)
        Employee employee = (Employee)subject.getPrincipal();
        //System.out.println(principal);

        //判断是否是超级管理员
        if (employee.isAdmin()) {
            info.addRole("ADMIN");//管理员有管理员角色
            info.addStringPermission("*:*");//如果是管理员就有全部权限
        } else {
            //根据id获取用户拥有的角色的sn编码
            List<Role> roles = roleMapper.selectByEmpId(employee.getId());
            ArrayList<String> rolesSnList = new ArrayList<>();
            for (Role role : roles) {
                rolesSnList.add(role.getSn());
            }
            info.addRoles(rolesSnList);

            //根据id获取用户拥有的权限表达式
            List<String> permissions = permissionMapper.selectByEmpId(employee.getId());
            info.addStringPermissions(permissions);
        }
        return info;
    }
}
